package controllers

import (
	"encoding/json"
	"strconv"
	"supershare/backend/models"

	"github.com/beego/beego/v2/server/web"
)

// CleanerController 保洁人员控制器
type CleanerController struct {
	web.Controller
}

// GetOne 获取单个保洁人员信息
// @Title GetOne
// @Description get cleaner by id
// @Param	id	path	string	true	"The key for staticblock"
// @Success 200 {object} models.Cleaner
// @Failure 403 :id is empty
// @router /:id [get]
func (c *CleanerController) GetOne() {
	id, _ := strconv.ParseInt(c.Ctx.Input.Param(":id"), 10, 64)
	cleaner, err := models.GetCleanerById(id)
	if err != nil {
		c.Data["json"] = ErrorResponse(err.Error())
	} else {
		c.Data["json"] = SuccessResponse(cleaner)
	}
	c.ServeJSON()
}

// List 获取保洁人员列表
// @Title List
// @Description get cleaner list
// @Param	page	query	int	false	"Page number"
// @Param	pageSize	query	int	false	"Page size"
// @Param	name	query	string	false	"Cleaner name"
// @Param	phone	query	string	false	"Cleaner phone"
// @Param	status	query	int	false	"Cleaner status"
// @Param	franchise_id	query	int64	false	"Franchise ID"
// @Param	store_id	query	int64	false	"Store ID"
// @Success 200 {object} map[string]interface{}
// @router / [get]
func (c *CleanerController) List() {
	// 检查权限
	permResult := CheckControllerPermission(c.Ctx)
	if !permResult.Success {
		RespondWithError(&c.Controller, permResult)
		return
	}

	page, _ := c.GetInt("page", 1)
	pageSize, _ := c.GetInt("pageSize", 10)
	filters := make(map[string]interface{})
	if name := c.GetString("name"); name != "" {
		filters["name__icontains"] = name
	}
	if phone := c.GetString("phone"); phone != "" {
		filters["phone__icontains"] = phone
	}
	if status, err := c.GetInt8("status"); err == nil {
		filters["status"] = status
	}
	if storeId, err := c.GetInt64("store_id"); err == nil {
		filters["store_ids__icontains"] = storeId
	}
	if permResult.Admin.FranchiseId > 0 {
		filters["franchise_id"] = permResult.Admin.FranchiseId
	}
	cleaners, total, err := models.GetCleanerList(page, pageSize, filters)
	if err != nil {
		c.Data["json"] = ErrorResponse(err.Error())
	} else {
		c.Data["json"] = SuccessResponse(map[string]interface{}{
			"list":  cleaners,
			"total": total,
		})
	}
	c.ServeJSON()
}

// Create 创建保洁人员
// @Title Create
// @Description create cleaner
// @Param	body	body	models.Cleaner	true	"body for cleaner content"
// @Success 201 {int} models.Cleaner.Id
// @Failure 403 body is empty
// @router / [post]
func (c *CleanerController) Create() {
	// 检查权限
	permResult := CheckControllerPermission(c.Ctx)
	if !permResult.Success {
		RespondWithError(&c.Controller, permResult)
		return
	}

	var cleaner models.Cleaner
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &cleaner); err != nil {
		c.Data["json"] = ErrorResponse(err.Error())
		c.ServeJSON()
		return
	}
	cleaner.FranchiseId = permResult.Admin.FranchiseId
	id, err := models.AddCleaner(&cleaner)
	if err != nil {
		c.Data["json"] = ErrorResponse(err.Error())
	} else {
		c.Data["json"] = SuccessResponse(id)
	}
	c.ServeJSON()
}

// Update 更新保洁人员信息
// @Title Update
// @Description update the cleaner
// @Param	id	path	string	true	"The id you want to update"
// @Param	body	body	models.Cleaner	true	"body for cleaner content"
// @Success 200 {object} models.Cleaner
// @Failure 403 :id is not int
// @router /:id [put]
func (c *CleanerController) Update() {
	// 检查权限
	permResult := CheckControllerPermission(c.Ctx)
	if !permResult.Success {
		RespondWithError(&c.Controller, permResult)
		return
	}

	id, _ := strconv.ParseInt(c.Ctx.Input.Param(":id"), 10, 64)
	var cleaner models.Cleaner
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &cleaner); err != nil {
		c.Data["json"] = ErrorResponse(err.Error())
		c.ServeJSON()
		return
	}
	cleaner.Id = id
	if err := models.UpdateCleaner(&cleaner); err != nil {
		c.Data["json"] = ErrorResponse(err.Error())
	} else {
		c.Data["json"] = SuccessResponse("Updated successfully")
	}
	c.ServeJSON()
}

// Delete 删除保洁人员
// @Title Delete
// @Description delete the cleaner
// @Param	id	path	string	true	"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *CleanerController) Delete() {
	id, _ := strconv.ParseInt(c.Ctx.Input.Param(":id"), 10, 64)
	// 检查权限
	permResult := CheckControllerPermission(c.Ctx)
	if !permResult.Success {
		RespondWithError(&c.Controller, permResult)
		return
	}
	// 检查权限
	cleaner, err := models.GetCleanerById(id)
	if err != nil {
		c.Data["json"] = ErrorResponse(err.Error())
		c.ServeJSON()
		return
	}
	if cleaner.FranchiseId != permResult.Admin.FranchiseId {
		c.Data["json"] = ErrorResponse("Permission denied")
		c.ServeJSON()
		return
	}
	if err := models.DeleteCleaner(id); err != nil {
		c.Data["json"] = ErrorResponse(err.Error())
	} else {
		c.Data["json"] = SuccessResponse("Deleted successfully")
	}
	c.ServeJSON()
}
